package asionreachel.cn.smarthomedemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import asionreachel.cn.smarthomedemo.R;
import asionreachel.cn.smarthomedemo.base.BaseActivity;
import asionreachel.cn.smarthomedemo.base.Common;
import asionreachel.cn.smarthomedemo.base.ConstantDefinition;


/**
 * Created by SuperMa on 2017/6/27.
 */

public class EditTextInfoActivity extends BaseActivity {
    private Button m_btCancel;
    private Button m_btyes;
    private EditText m_etText;
    private TextView m_tvTips;
    private TextView m_tvPlace;
    private int maxEms = 16;
    private String m_strParm;
    private String m_strContent;
    private int m_iResultCode;
    private TextView m_tvTitle;
    private String m_strTitle;
    public static OnEditItemClickListener m_editItemClickListener;
    protected View vBaseBottomWindowRoot;//子Activity全局背景View
    /**
     * 退出时之前的界面进入动画,可在finish();前通过改变它的值来改变动画效果
     */
    public interface OnEditItemClickListener {

        /**点击item事件的回调方法
         * @param requestCode 传入的用于区分某种情况下的showDialog
         * @param a_strParm
         */
        void onEditItemClick(int requestCode, String a_strContent, String a_strParm);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        m_strParm = intent.getStringExtra(ConstantDefinition.EDIT_PARM);       //自带参数
        m_strContent = intent.getStringExtra(ConstantDefinition.EDIT_CONTENT);  //显示内容
        m_strTitle = intent.getStringExtra(ConstantDefinition.EDIT_TITLE);      //标题
        m_iResultCode = intent.getIntExtra(ConstantDefinition.EDIT_RESULT_CODE , 0);
        maxEms = intent.getIntExtra(ConstantDefinition.EDIT_MAX_EMS , maxEms);
        setContentView(R.layout.edit_text_info_window);
        m_btCancel = (Button)findViewById(R.id.edit_text_cancel);
        m_btyes = (Button)findViewById(R.id.edit_text_yes);
        m_etText = (EditText)findViewById(R.id.etEditTextInfo);
        m_tvTips = (TextView)findViewById(R.id.tvEditTextInfoRemind);
        m_tvPlace = (TextView)findViewById(R.id.tvEditTextInfoPlace);
        m_tvPlace.setVisibility(View.GONE);
        m_btCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onCancel();
            }
        });
        m_btyes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onYes();
            }
        });
        m_etText.setText(m_strContent);
        m_etText.setMaxEms(maxEms);
        m_tvTips.setText("限" + maxEms + "个字符");
        m_tvTitle = (TextView)findViewById(R.id.edit_text_title);
        m_tvTitle.setText(m_strTitle);
        m_etText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //m_tvTips.setText("限" + (maxEms - count)/2 + "个字（或" + (maxEms - count) + "个字符）");

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (Common.getWordCount(m_etText.getText().toString()) > maxEms)
                {
                    try {
                        String subStr = Common.subString(m_etText.getText().toString(), maxEms);
                        m_etText.setText(subStr);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        //enterAnim = exitAnim;
        //vBaseBottomWindowRoot = findViewById(R.id.vBaseBottomWindowRoot);

        //vBaseBottomWindowRoot.startAnimation(AnimationUtils.loadAnimation(mActivity, R.anim.bottom_window_enter));
    }

    private void onCancel()
    {
        finish();
    }

    private void onYes()
    {
        String strChangeName = m_etText.getText().toString().trim();
        if (strChangeName.isEmpty() || strChangeName.equals(getIntent().getStringExtra(ConstantDefinition.EDIT_CONTENT)))
        {
            Common.showToast("内容没有改变");
            return;
        }
        //修改名称条件符合
        if (m_editItemClickListener != null)
            m_editItemClickListener.onEditItemClick(m_iResultCode , strChangeName ,m_strParm );
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        //Common.hideKeyboard(mActivity, m_etText);
        //vBaseBottomWindowRoot.startAnimation(AnimationUtils.loadAnimation(mActivity, R.anim.bottom_window_exit));
        //vBaseBottomWindowRoot.setVisibility(View.GONE);
    }

    public static void setEditItemClickListener(OnEditItemClickListener a_editClicklistener)
    {
        m_editItemClickListener = a_editClicklistener;
    }

}
